package com.wu.project.admin.service.impl;

import com.wu.project.admin.component.JsonResult;
import com.wu.project.admin.dao.UserDao;
import com.wu.project.admin.entity.AddRoleMenuVO;
import com.wu.project.admin.entity.RoleEntity;
import com.wu.project.admin.entity.UserEntity;
import com.wu.project.admin.service.ShiroUserService;
import com.wu.project.admin.util.CommonResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author xuan
 */
@Service("shiroService")
public class ShiroUserServiceImpl implements ShiroUserService {

    @Autowired
    UserDao userDao;

    @Override
    public UserEntity findUserByName(String username) {
        return userDao.queryUserByName(username);
    }

    @Override
    public String getRolesByUid(Long userId) {
        List<RoleEntity> roleEntities = userDao.queryRoleList(userId);
        Set<String> roleSet = new HashSet<>();
        for (RoleEntity roleEntity : roleEntities) {
            roleSet.add(roleEntity.getRname());
        }
        return String.join(",", roleSet);
    }

    @Override
    public Set<String> getPermsByUid(Long userId) {
        return null;
    }

    @Override
    public JsonResult getUserMenu(String uid) {
        return CommonResultUtil.Instance.success(userDao.queryUserMenu(uid));
    }

    @Override
    public JsonResult getAllMenu() {
        return CommonResultUtil.Instance.success(userDao.queryAllMenu());
    }

    @Override
    public JsonResult addRoleMenu(AddRoleMenuVO addRoleMenuVO) {
        if (addRoleMenuVO.getMenuIds().size() == 0){
            return CommonResultUtil.Instance.success();
        }
        Integer integer = userDao.addRoleMenu(addRoleMenuVO);
        return CommonResultUtil.Instance.success();
    }
}
